
def solution(n, max_val, array):
    freq = {}
    for card in array:
        freq[card] = freq.get(card, 0) + 1

    order = [1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
    a_candidates = []
    b_candidates = []

    for card in order:
        if freq.get(card, 0) >= 3:
            a_candidates.append(card)
        if freq.get(card, 0) >= 2:
            b_candidates.append(card)

    for a in a_candidates:
        for b in b_candidates:
            if b != a and (3 * a + 2 * b) <= max_val:
                return [a, b]

    return [0, 0]

if __name__ == "__main__":
    # 测试用例
    result1 = solution(9, 34, [6, 6, 6, 8, 8, 8, 5, 5, 1])
    print(result1 == [8, 5])  # 应输出True

    result2 = solution(9, 37, [9, 9, 9, 9, 6, 6, 6, 6, 13])
    print(result2 == [6, 9])  # 应输出True

    result3 = solution(9, 40, [1, 11, 13, 12, 7, 8, 11, 5, 6])
    print(result3 == [0, 0])  # 应输出True